package com.lyj.ai.controller;

import com.lyj.ai.mapper.UserMapper;
import com.lyj.ai.tools.DateTimeTools;
import com.lyj.ai.tools.StudentTools;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.model.tool.ToolCallingChatOptions;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.ai.tool.ToolCallbacks;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;

/**
 * 函数调用
 * 主要有两个作用：1，信息检索：比如检索最新的时间；2，采取某个行动，比如帮我们发送邮件，下单等
 */
@RestController
public class FunctionController {


    @Autowired
    ChatModel chatModel;

    @Autowired
    UserMapper mapper;

    /**
     * 通过调用函数方法，让模型检索时间
     * @param msg
     * @return
     */
    @GetMapping(value = "/ai/function")
    public  String functionTest(@RequestParam(value = "msg",defaultValue = "今天几月几号") String msg ) {

        String response = ChatClient.create(chatModel)
                .prompt(msg)
                // 函数调用
                .tools(new DateTimeTools())
                .call()
                .content();

        return response;
    }

    @GetMapping(value = "/ai/function/chatModel")
    public  String byChatModel(@RequestParam(value = "msg",defaultValue = "今天几月几号") String msg ) {
        ToolCallback[] dateTimeTools = ToolCallbacks.from(new DateTimeTools());
        ChatOptions chatOptions = ToolCallingChatOptions.builder()
                .toolCallbacks(dateTimeTools)
                .build();
        Prompt prompt = new Prompt("What day is tomorrow?", chatOptions);
        String response = chatModel.call(prompt).getResult().getOutput().getText();

        return response;
    }



    @GetMapping(value = "/ai/nofunction")
    public  String noFunctionTest(@RequestParam(value = "msg",defaultValue = "今天几月几号") String msg ) {

        String response = ChatClient.create(chatModel)
                .prompt()
//                .tools(new DateTimeTools())
                .call()
                .content();

        System.out.println(response);
        return response;
    }

    /**
     * 让模型采取行动，比如指定一个10分钟后的闹钟
     * @param msg
     * @return
     */
    @GetMapping(value = "/ai/modelDoSomeThing")
    public  String modelDoSomeThing(@RequestParam(value = "msg",defaultValue = "设置一个10分钟后的闹钟") String msg ) {

        String response = ChatClient.create(chatModel)
                .prompt(msg)
                // 函数调用
                .tools(new DateTimeTools())
                .call()
                .content();

        return response;
    }


    @GetMapping(value = "/ai/find" , produces = "text/markdown;charset=UTF-8")
    public  Flux<String> studentInfo(@RequestParam(value = "msg",defaultValue = "李四是谁") String msg ) {

        return ChatClient.create(chatModel)
                .prompt(msg)
                // 函数调用
                .tools(new StudentTools(mapper))
                .stream()
                .content();

    }
}
